package com.ruoyi.activitidemo;

import com.ruoyi.activiti.*;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.service.ConfigService;
import com.ruoyi.system.domain.ActivitiLeave;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

//最最基础的事件控制
@Component
@DependsOn("baseController")
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ProcessController extends BaseController {

    //测试service调用
    @Autowired
    private ConfigService configService;


    @Autowired
    private SysUserServiceImpl userService;

    public void test()
    {
        System.out.println("yanruTODO 测试的");
        System.out.println("测试参数："+configService.getKey("sys.account.registerUser"));



    }

    //添加实例，添加流程实例
    public void addInstance(SysUser sysUser, ActivitiLeave activitiLeave)
    {
        System.out.println("发起请假的用户="+sysUser);
        //1.得到ProcessEngine对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到RunService对象
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //3.创建流程实例,同时要添加业务标识BusinessKey(第二个参数)，假设请假单的id是10001， 这个案例比基础的启动流程多了 第二个参数
        //其中第一个参数是流程定义的id，可不是流程bpmn的文件名哦，具体看该bpmn文件编辑页面的id属性的value是多少，
        // 创建流程变量, yanruTODO奇怪，这个参数 启动流程之后查不到参数数据
        Map<String,Object> variables = new HashMap<String,Object>();
        variables.put("addInstance请假人", sysUser.getUserName());
        variables.put("请假单id", activitiLeave.getId());
        //yanruTODO这个流程指定谁去审批，userId是指定的审批人
        //查找查找部门经理
        SysUser sysUser1=  this.userService.selectUserByLoginName("经理张晓晓");

        System.out.println("指定请假单第一个审批人user="+sysUser1.getLoginName());

        variables.put("userId", sysUser1.getUserId()); //yanruTODO 这个userId 就是assignee 属性！！！
        variables.put("inputUser", "有问题的参数");
        //现在idea的bpmn插件有bug，我编辑添加了这个属性，但是面板保存后就看不到这个属性了，但是实际这个变量可能添加成功了


        String businessKey = activitiLeave.getId()+"";
        // 在启动时设置流程变量
        ProcessInstance holiday = runtimeService.startProcessInstanceByKey("myProcess_1" , businessKey, variables);
        //4.输出流程实例的相关信息
        System.out.println("部署ID："+holiday.getDeploymentId()+",此时请假单id="+activitiLeave.getId());
        System.out.println("名称："+holiday.getName());
        System.out.println("ID："+holiday.getId());
        System.out.println("BusinessKey："+holiday.getBusinessKey());
        System.out.println("实例ID："+holiday.getActivityId());
        System.out.println("实例的参数:"+holiday.getProcessVariables().toString());
        System.out.println("准备放入参数:"+variables);

        //yanruTODO 修改请假参数

        //yanruTODO 其他测试函数走一遍
        QueryProcessDefinition.queryProcess();

        //查询历史
        HistoryQuery.queryHistory();

        //删除流程
       // String errorDelete = DeleteProcessDefinition.delete("60001");

        //查询任务
        Task task = ActivitiTaskQuery.queryTask(activitiLeave);

        //完成任务
      //  ActivitiCompleteTask.complete(task);
    }


}
